package system.base;

import java.util.Map;

import framework.core.*;
import system.data.*;
/*
	Author    :Arrow
	CreateTime:2009-05-14 15:27:41
*/
public class BillMonthIoSrv extends IHandler
{

	public BillMonthIoSrv() 
	{
		super();
	}
	private void deleteBillMonthIo(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		BillMonthIo obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = BillMonthIo.fetch(con,ids[i]);
			if(obj!=null){
			    obj.setMoney(0);
			    obj.save(con);
			}
		}
	}
	private void saveBillMonthIo(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		Map map = ConstDefine.initOtherMoneyType();
	    
	    String year = context.getString("Year", "");
		String month = context.getString("Month", "");
		String warehouseCode = context.getString("WarehouseCode", "");
		Warehouse warehouse = Warehouse.fetchByCode(con, warehouseCode);
        if(warehouse == null) warehouse = Warehouse.fetch();
	    
	    BillMonthIo obj;
		BillMonthIoPageData data = (BillMonthIoPageData)PageData.fromForm(context,BillMonthIoPageData.class);
		context.setValue("BillMonthIoPageData", null);
		
		for(int i = 0; i < ConstDefine.OtherMoneyType.length; i++){
		    String type = ConstDefine.OtherMoneyType[i];
		    double money = context.getDouble("Money_" + i, 0.00);
		    obj = BillMonthIo.fetch(con, warehouseCode, year + "-" + month, type);
		    if(obj == null) obj = BillMonthIo.fetch();
		    
		    String id = obj.getId();
		    if((id==null)||(id.equals("")))   //add
	        {
	            obj = BillMonthIo.fetch();
	        }
	        else                     //update
	        {
	            obj = BillMonthIo.fetch(con,id);
	        }
		    
		    obj.setMoney(money);
		    obj.setWarehouseCode(warehouseCode);
		    obj.setWarehouseName(warehouse.getName());
		    obj.setMoneyType(type);
		    obj.setMoneyIo((String)map.get(type));
		    obj.setMoneyDate(year + "-" + month);
		    obj.setCreateUserId(loginContext.getLoginUserId());
		    obj.setCreateUser(loginContext.getLoginUserName());
		    obj.setCreateTime(new DateUtil().getFullString());
		    
		    obj.save(con);
		}
	}
	private void listBillMonthIo(DBConnection con,SrvContext context,ILoginContext loginContext, String path) throws Exception
	{
		BillMonthIoSearchEvent event = (BillMonthIoSearchEvent)SearchEvent.fromForm(context,BillMonthIoSearchEvent.class);
		if("".equals(event.getWarehouseCode())){
		    event.setWarehouseCode(loginContext.getLoginWarehouseCode());
		}
		context.setSessionValue("BillMonthIoSearchEvent",event);
		context.setNextUrl(path+"BillMonthIoList.jsp");
	}
	private void backBillMonthIo(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"BillMonthIoList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listBillMonthIo"))
		{
			listBillMonthIo(con,context, loginContext, path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewBillMonthIo"))
		{
			String id = context.getString("Id",null);
			BillMonthIo obj = BillMonthIo.fetch(con,id);
			context.setValue("BillMonthIo",obj);
			context.setNextUrl(path+"BillMonthIoView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteBillMonthIo"))
		{
			context.setErrorUrl(path+"BillMonthIoList.jsp");
			deleteBillMonthIo(con,context);
			backBillMonthIo(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddBillMonthIo"))
		{
			BillMonthIo obj = BillMonthIo.fetch();
			BillMonthIoPageData data = (BillMonthIoPageData)PageData.fromTable(obj,BillMonthIoPageData.class);
			context.setValue("BillMonthIoPageData",data);
			context.setNextUrl(path+"BillMonthIoNew.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateBillMonthIo"))
		{
			String id = context.getString("Id",null);
			BillMonthIo obj = BillMonthIo.fetch(con,id);
			BillMonthIoPageData data = (BillMonthIoPageData)PageData.fromTable(obj,BillMonthIoPageData.class);
			context.setValue("BillMonthIoPageData",data);
			context.setNextUrl(path+"BillMonthIoEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("updateBillMonthIo"))
        {
		    context.setErrorUrl(path+"BillMonthIoEdit.jsp");
            //saveBillMonthIo(con,context,loginContext);
		    
		    String id = context.getString("Id",null);
            BillMonthIo obj = BillMonthIo.fetch(con,id);
		    double money = context.getDouble("Money", 0);
            obj.setMoney(money);
            obj.save(con);
            backBillMonthIo(con,context,path);
            return;
        }
		if(action_code.equalsIgnoreCase("toCopyBillMonthIo"))
		{
			String id = context.getString("Id",null);
			BillMonthIo obj = BillMonthIo.copy(BillMonthIo.fetch(con,id));
			BillMonthIoPageData data = (BillMonthIoPageData)PageData.fromTable(obj,BillMonthIoPageData.class);
			context.setValue("BillMonthIoPageData",data);
			context.setNextUrl(path+"BillMonthIoEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveBillMonthIo"))
		{
			context.setErrorUrl(path+"BillMonthIoNew.jsp");
			saveBillMonthIo(con,context,loginContext);
			backBillMonthIo(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewBillMonthIo"))
		{
			context.setErrorUrl(path+"BillMonthIoEdit.jsp");
			saveBillMonthIo(con,context,loginContext);
			BillMonthIo obj = BillMonthIo.fetch();
			BillMonthIoPageData data = (BillMonthIoPageData)PageData.fromTable(obj,BillMonthIoPageData.class);
			context.setValue("BillMonthIoPageData",data);
			context.setNextUrl(path+"BillMonthIoEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListBillMonthIo"))
		{
			backBillMonthIo(con,context,path);
			return;
		}
		
		//add by Arrow start

		//add by Arrow end

		Logger.throwException("BillMonthIoSrv","unknown action_code="+action_code);
	}
}
